#region

using Gurock.SmartInspect;
using Microsoft.Practices.Unity.InterceptionExtension;

#endregion

namespace SmartInspect.Extensions.Unity
{
    public class SiMethodCallHandler : SiWrappedCallHandler
    {
        private bool _logEnabled;
        public bool EnableAccessorMethodLogging { private get; set; }

        protected override void Enter(IMethodInvocation input, Session ses)
        {
            _logEnabled = (!input.IsPropertyAccessorMethodCall() || EnableAccessorMethodLogging) &&
                !(GetTypeName(input.MethodBase.DeclaringType) == "System.Object" && input.MethodBase.Name == "GetType");
            if(!_logEnabled)
                return;
            string methodName = FormatTitle(input.MethodBase);
            LogArgs(methodName, input, ses, LogEntryType.EnterMethod);
            ses.SendCustomProcessFlow(methodName, ProcessFlowType.EnterMethod);
        }

        protected override void Leave(IMethodInvocation input, Session ses, IMethodReturn ret)
        {
            if(!_logEnabled)
                return;
            string methodName = FormatTitle(input.MethodBase);
            LogOutputs(methodName, input, ret, ses, LogEntryType.LeaveMethod);
            ses.SendCustomProcessFlow(methodName, ProcessFlowType.LeaveMethod);
        }
    }
}